from PyQt5.QtWidgets import QWidget
from PyQt5.QtCore import Qt, QPoint


class ControllerTag(QWidget):
    # 父对象
    Parent = None
    # 索引
    Index = 0
    # 所属控制器
    controller = None
    # 坐标
    xRate = 0
    yRate = 0

    def __init__(self, parent, controller=None):
        super().__init__(parent)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setCursor(Qt.PointingHandCursor)
        self.Parent = parent
        self.controller = controller

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            print('点击')
            controller = self.controller
            controller.Active = True
            controller.OperatingTag = self
            event.accept()

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            print('释放')
            controller = self.controller
            controller.Active = False
            controller.OperatingTag = None
            event.accept()

    def move2(self, x, y):
        """
        移动部件 中心到（xRate,yRate)
        :param x: int
        :param y: int
        :return:
        """
        self.xRate = x / self.Parent.width()
        self.yRate = y / self.Parent.height()
        x = x - self.width() / 2
        y = y - self.height() / 2
        self.move(x, y)

    def updatePos(self):
        x = self.Parent.width() * self.xRate
        y = self.Parent.height() * self.yRate
        x = x - self.width() / 2
        y = y - self.height() / 2
        self.move(x, y)

    @property
    def pos2(self):
        """
        中心坐标
        :return:
        """
        return QPoint(self.x() + self.width()/2, self.y() + self.height()/2)

    @property
    def x2(self):
        """
        中心坐标
        :return:
        """
        return self.x() + self.width()/2

    @property
    def y2(self):
        """
        中心坐标
        :return:
        """
        return self.y() + self.height() / 2
